## Section 4 of the Online Appendix: continuity-based approach

library(dplyr)
library(ggplot2)
library(gridExtra)
library(grid)
library(reshape2)
library(grid)
library(rdrobust)
library(rdlocrand)
library(lubridate)


## Clear workspace

rm=list(ls())

##Opening the voting data:

load("ReplicationData.RData")

## Turnout variable
data2010$turnout <- as.numeric(data2010$VOTOU.1º.TURNO) - 1


############
### 2010 ###
############

#################################################################
## Discontinuity analyses treating the forcing variable as ######
## continuous and adopting the RD Honest approach          ######
#################################################################

## Election Day: use balanced windows from July 07 through December 31 (cutpoint is October 03), 89 days on each side

data.18.rddED <- dplyr::filter(data2010, (dob >= "1992-07-07" & dob <= "1992-12-31") & turnout == 1)

DataRDDED18 <- data.18.rddED %>%
  filter(GRAU.INSTRUÇÃO != "Analfabeto") %>%
  group_by(dob) %>%
  summarise(voters = n(),
            turnout = unique(turnout))

DataRDDED18$daysToFrom <- seq(88, -89, by = -1)


## RDD using MSE-optimal bandwidth selection and p = 1

rdd.18ED <- rdrobust(y = DataRDDED18$voters, x = DataRDDED18$daysToFrom, c = 0, p = 1, bwselect = "mserd")
summary(rdd.18ED)
rdd.18ED$beta_p_r
rdd.18ED$beta_p_l

## % increase
(percentIncreaseED <- ((rdd.18ED$beta_p_r[1,]-rdd.18ED$beta_p_l[1,])/rdd.18ED$beta_p_l[1,])*100)

## % increase: lower bound
(lowerED <- (rdd.18ED$ci[3,1]/rdd.18ED$beta_p_l[1,])*100)

## % increase: upper bound
(upperED <- (rdd.18ED$ci[3,2]/rdd.18ED$beta_p_l[1,])*100)


## End-of-year: use balanced windows from October 04 through March 30 (cutpoint is December 31), 89 days on each side

data.18.rddEoY <- dplyr::filter(data2010, (dob >= "1992-10-04" & dob <= "1993-03-30") & turnout == 1)

DataRDDEoY18 <- data.18.rddEoY %>%
  filter(GRAU.INSTRUÇÃO != "Analfabeto") %>%
  group_by(dob) %>%
  summarise(voters = n(),
            turnout = unique(turnout))

DataRDDEoY18$daysToFrom <- seq(88, -89, by = -1)


## RDD using MSE-optimal bandwidth selection and p = 1

rdd.18EoY <- rdrobust(y = DataRDDEoY18$voters, x = DataRDDEoY18$daysToFrom, c = 0, p = 1, bwselect = "mserd")
summary(rdd.18EoY)
rdd.18EoY$beta_p_r
rdd.18EoY$beta_p_l

## % increase
(percentIncreaseEoY <- ((rdd.18EoY$beta_p_r[1,]-rdd.18EoY$beta_p_l[1,])/rdd.18EoY$beta_p_l[1,])*100)

## % increase: lower bound
(lowerEoY <- (rdd.18EoY$ci[3,1]/rdd.18EoY$beta_p_l[1,])*100)

## % increase: upper bound
(upperEoY <- (rdd.18EoY$ci[3,2]/rdd.18EoY$beta_p_l[1,])*100)






